

function CreateEnemy ( )
    // Load image if it has not been already
    if eSaucerImage = 0 then eSaucerImage = LoadImage ( "arena/enemySaucer.png" )

    newEnemy = FindInactive ( )
    spawner = FindAvailableSpawner ( )
    if newEnemy <> 0 and spawner <> 0
        eSaucer [ newEnemy ].Sprite = CreateSprite ( 0 )
        eSaucer [ newEnemy ].Active = 1
        // Load images into enemy sprite for animation
        AddSpriteAnimationFrame ( eSaucer [ newEnemy ].Sprite, eSaucerImage )
        // Setup physics
        SetSpriteShape              ( eSaucer [ newEnemy ].Sprite, 3   )
        SetSpritePhysicsOn          ( eSaucer [ newEnemy ].Sprite, 2   )
        SetSpriteGroup              ( eSaucer [ newEnemy ].Sprite, 2   )
        SetSpritePhysicsCanRotate   ( eSaucer [ newEnemy ].Sprite, 0   )
        SetSpritePhysicsRestitution ( eSaucer [ newEnemy ].Sprite, 0.2 )
        SetSpritePhysicsMass        ( eSaucer [ newEnemy ].Sprite, 1   )
        SetSpritePhysicsOff         ( eSaucer [ newEnemy ].Sprite      )
        // Place saucer at spawner
        SetSpritePositionByOffset ( eSaucer [ newEnemy ].Sprite, GetSpriteXByOffset ( g_iSpawner [ spawner ] ), GetSpriteYByOffset ( g_iSpawner [ spawner ] ) )
        eSaucer [ newEnemy ].TargetX = GetSpriteXByOffset ( g_iSpawner [ spawner ] )
        eSaucer [ newEnemy ].TargetY = GetSpriteYByOffset ( g_iSpawner [ spawner ] ) - 100
        // Initialize new enemy
        ResetEnemy ( newEnemy )
    endif
    if newEnemy <> 0 and spawner = 0
        AddToSpawnQueue ( 1 )
    endif
endfunction

function ResetEnemy ( enemy )
    eSaucer [ enemy ].MaxVel  = 400
    eSaucer [ enemy ].State   = Spawning
    eSaucer [ enemy ].aiType  = 1
    eSaucer [ enemy ].aiState = 2
    if GetSpriteYByOffset ( eSaucer [ enemy ].Sprite ) < 435 + 30 then eSaucer [ enemy ].Zone = 3
    if GetSpriteYByOffset ( eSaucer [ enemy ].Sprite ) < 330 + 30 then eSaucer [ enemy ].Zone = 2
    if GetSpriteYByOffset ( eSaucer [ enemy ].Sprite ) < 140 + 30 then eSaucer [ enemy ].Zone = 1
    eSaucer [ enemy ].StateTimerGoal = timer() + SpawnDelay
    eSaucer [ enemy ].StateTimer     = timer()
endfunction

function DestroyEnemy ( enemy )
    SetSpritePhysicsDelete ( eSaucer [ enemy ].Sprite )
    DeleteSprite           ( eSaucer [ enemy ].Sprite )
    eSaucer [ enemy ].Active = 0
endfunction

function UpdateEnemy ( enemy )
    select ( eSaucer [ enemy ].State )

        case Spawning:
            time# = timer()
            if time# < eSaucer [ enemy ].StateTimerGoal
                // Total vertical movement = Sprite Height       Scaling straight percentage
                SetSpritePositionByOffset ( eSaucer [ enemy ].Sprite , GetSpriteXByOffset ( eSaucer [ enemy ].Sprite ), GetSpriteYByOffset ( eSaucer [ enemy ].Sprite ) - ( ( 30.0 / SpawnDelay ) * ( time# - eSaucer [ enemy ].StateTimer ) ) )
                //SetSpriteScaleByOffset ( eSaucer [ enemy ].Sprite, 1.0 - ( eSaucer [ enemy ].StateTimerGoal - time# ) / SpawnDelay, 1.0 - ( eSaucer [ enemy ].StateTimerGoal - time# ) / SpawnDelay )
                eSaucer [ enemy ].StateTimer = time#
            else
                // Finished spawning
                eSaucer [ enemy ].State = Idle
                eSaucer [ enemy ].StateTimer = timer()
                eSaucer [ enemy ].StateTimerGoal = 0.2
                SetSpriteScaleByOffset ( eSaucer [ enemy ].Sprite, 1, 1 )
                SetSpritePhysicsOn ( eSaucer [ enemy ].Sprite, 2 )
            endif
        endcase

        case Dying:
            time# = timer()
            if time# < eSaucer [ enemy ].StateTimerGoal
                //
            else
                DestroyEnemy ( enemy )
                AddToSpawnQueue ( 1 )
            endif
        endcase

        case Idle:
            MoveEnemy ( enemy )
        endcase

        case Moving:
            eSaucer [ enemy ].State = Idle
        endcase

    endselect
endfunction

function MoveEnemy ( enemy )
    if timer() - eSaucer [ enemy ].StateTimer > eSaucer [ enemy ].StateTimerGoal
        if eSaucer [ enemy ].aiType = 1 then AIInput ( enemy )
        eSaucer [ enemy ].StateTimer = timer()
    endif
    CheckBoundary ( eSaucer [ enemy ].Sprite, 800, -64, 480, -64)
    CapSpeed ( eSaucer [ enemy ].Sprite, eSaucer [ enemy ].MaxVel )
endfunction

function FindInactive ( )
    for enemy = 1 to MaxEnemies
        if eSaucer [ enemy ].Active = 0 then exitfunction enemy
    next enemy
endfunction 0

function CheckForSpawn ( )
    for index = 1 to MaxEnemies
        if eSaucer [ index ].Active = 1
            if eSaucer [ index ].State = Spawning then exitfunction 1
        endif
    next index
    if pSaucer.Active = 1
        if pSaucer.State = Spawning then exitfunction 1
    endif
endfunction 0
